<?php

namespace app\api\controller;

use think\Controller;

class Edition extends Controller
{
    /**
     * 检测版本
     */
    public function check_edition($header, $data, $user)
    {
        $t_edition = db('edition')
            ->where(array('edition' => $header['edition'], 'type' => $header['phone_model'], 'status' => 1))
            ->find();
        //当前版本
        if (empty($t_edition)) {
            $t_edition['sort'] = 0;
        }
        $new = db('edition')
            ->where(array('type' => $header['phone_model'], 'status' => 1,'sort'=>array('gt',$t_edition['sort'])))
            ->order("sort desc")
            ->find();
        //最新的版本
        if (empty($new)) {
            //没有更新的版本了
            return array('code' => 1, 'msg' => '已是最新版本', 'data' => array('need'=>0));
        }
        $return = array(
            'need' => 1,//是否需要更新
            'is_force' => $new['is_force'],//是否强制：1：强制更新
            'download_url' => $new['down_url'],//文件下载地址
            'remote_url' => $new['remote_url'],//远程下载地址
        );
        $force = db('edition')
            ->where(array('type' => $header['phone_model'], 'status' => 1, 'is_force' => 1,'sort'=>array('gt',$t_edition['sort'])))
            ->order("sort desc")
            ->find();
        //最新的强制更新版本
        if (!empty($force)) {
            $return['is_force'] = 1;
        }
        return array('code' => 1, 'msg' => '需要更新', 'data' => $return);
    }
}